Version 1/100817 of Trainer Message Queues by Juhana Leinonen begins here.


Chapter Definitions

A message queue is a kind of object.

The active queue is a message queue that varies.

Queue-priority is a kind of value. Queue-priorities are negligible, low, normal, high, and critical.

Table of Message Queue Data
Queue name	Queue-msg-text	Queue-msg-priority	Queue rule	FIFO counter	Queue-voice
message queue	indexed text	queue-priority	rule	number	person
with 30 blank rows.

Latest FIFO counter is a number that varies. Latest FIFO counter is 0.


Chapter Adding messages to the queue

To add (queue-msg - indexed text) to the/-- (queue-object - a message queue) as/with (priority-level - a queue-priority) priority/--:
	if the number of blank rows in Table of Message Queue Data is 0:
		say "** Programming error: Too many messages in queue **";
		stop;
	choose a blank row in Table of Message Queue Data;
	now queue name entry is queue-object;
	now queue-msg-text entry is queue-msg;
	now queue-msg-priority entry is priority-level;
	increase latest FIFO counter by 1;
	now FIFO counter entry is latest FIFO counter.

To add (queue-msg - indexed text) to the/-- (queue-object - a message queue) as/with (priority-level - a queue-priority) priority/-- spoken by (speaker - a person):
	if the number of blank rows in Table of Message Queue Data is 0:
		say "** Programming error: Too many messages in queue **";
		stop;
	choose a blank row in Table of Message Queue Data;
	now queue name entry is queue-object;
	now queue-msg-text entry is queue-msg;
	now queue-msg-priority entry is priority-level;
	now queue-voice entry is speaker;
	increase latest FIFO counter by 1;
	now FIFO counter entry is latest FIFO counter.

To add the/-- (queue-rule - rule) to the/-- (queue-object - a message queue) as/with (priority-level - a queue-priority) priority/--:
	if the number of blank rows in Table of Message Queue Data is 0:
		say "** Programming error: Too many messages in queue **";
		stop;
	choose a blank row in Table of Message Queue Data;
	now queue name entry is queue-object;
	now queue rule entry is queue-rule;
	now queue-msg-priority entry is priority-level;
	increase latest FIFO counter by 1;
	now FIFO counter entry is latest FIFO counter.

To add (queue-msg - indexed text) with (queue-rule - a rule) to the/-- (queue-object - a message queue) as/with (priority-level - a queue-priority) priority/--:
	if the number of blank rows in Table of Message Queue Data is 0:
		say "** Programming error: Too many messages in queue **";
		stop;
	choose a blank row in Table of Message Queue Data;
	now queue name entry is queue-object;
	now queue rule entry is queue-rule;
	now queue-msg-text entry is queue-msg;
	now queue-msg-priority entry is priority-level;
	increase latest FIFO counter by 1;
	now FIFO counter entry is latest FIFO counter.


[some shortcuts]
To add (queue-msg - indexed text) to the/-- (queue-object - a message queue):
	add queue-msg to the queue-object as normal priority.

To add (queue-msg - indexed text) to the/-- (queue-object - a message queue) spoken by (queue-voice - a person):
	add queue-msg to the queue-object as normal priority spoken by queue-voice.

To add (queue-rule - rule) to the/-- (queue-object - a message queue):
	add queue-rule to the queue-object as normal priority.

To add (queue-rule - a rule) with (queue-msg - indexed text) to the/-- (queue-object - a message queue) as/with (priority-level - a queue-priority) priority/--:
	add queue-msg with queue-rule to the queue-object as priority-level.

To add (queue-rule - rule) with (queue-msg - indexed text) to the/-- (queue-object - a message queue):
	add queue-msg with queue-rule to the queue-object as normal priority.

To add (queue-msg - indexed text) with (queue-rule - rule) to the/-- (queue-object - a message queue):
	add queue-msg with queue-rule to the queue-object as normal priority.


Chapter Showing messages from the queue

To show the/-- next message from the/-- (queue-object - a message queue):
	now the active queue is queue-object;
	let message-printed be false;
	let all-done be false;
	[first taking care of the critical messages]
	repeat through Table of Message Queue Data:
		if the queue name entry is the queue-object and the queue-msg-priority entry is critical:
			if queue-msg-text entry is not empty:
				carry out the displaying the message queue string activity with the queue-msg-text entry;
				say line break;
				now message-printed is true;
			otherwise:
				now message-printed is whether or not actually the queue rule entry printed something;
			if message-printed is true:
				blank out the whole row;
				now all-done is true;
	[if we printed at least one critical message, we don't display anything else but purge the negligible messages.]
	if all-done is true:
		purge all negligible messages from the queue-object;
		stop;
	[then on with the other messages. We'll loop through everything and remember the ones that have rules that didn't run.]
	if the Table of Message Queue Data is non-empty:
		display the highest priority message in queue-object ignoring {}.


To display the highest priority message in (queue-object - a message queue) ignoring (ignore-list - a list of numbers):
	let highest-priority be a queue-priority;
	let highest-priority be negligible;
	let lowest-FIFO-counter be 0;
	let chosen-row be -1;
	repeat with N running from 1 to the number of rows in Table of Message Queue Data:
		choose row N in the Table of Message Queue Data;
		if there is no queue name entry or queue name entry is not the queue-object or N is listed in ignore-list or ( there is no queue-msg-text entry and there is no queue rule entry ):
			next;
		if highest-priority is at most queue-msg-priority entry:
			if highest-priority is queue-msg-priority entry and lowest-FIFO-counter is greater than 0 and lowest-FIFO-counter is less than FIFO counter entry:
				do nothing;
			otherwise:
				now highest-priority is queue-msg-priority entry;
				now lowest-FIFO-counter is FIFO counter entry;
				now chosen-row is N;
	if chosen-row is not -1:
		choose row chosen-row in the Table of Message Queue Data;
		let print the text message be true;
		if there is a queue rule entry:
			if actually the queue rule entry printed something:
				now the print the text message is false;
				blank out the whole row;	
			otherwise:
				add chosen-row to ignore-list;
				display the highest priority message in queue-object ignoring ignore-list;
		if there is a queue-msg-text entry and print the text message is true:
			if there is a queue-voice entry:
				if the currently speaking character is the queue-voice entry:
					[if Evie has just spoken we'll skip showing the message this turn]
					if the currently speaking character is Evie:
						if the highest-priority < high:
							stop;
						otherwise if Evie says not mentioned is false:
							say ".[rpo]"; [Evie's lines don't have the full stop at the end so it has to be added]
					say paragraph break;
				say queue-voice entry speaking;
			carry out the displaying the message queue string activity with the queue-msg-text entry;
			if there is a queue-voice entry:
				rpo;
			otherwise:
				say line break;
			blank out the whole row;	



To decide whether actually the/-- (queue-rule - a rule) printed something:
	follow the queue-rule;
	if rule failed:
		decide no;
	decide yes.


Chapter Printing the message activity

Displaying the message queue string something is an activity on indexed text.

Rule for displaying the message queue string some indexed text (called the msg-string) (this is the print message queue string as it is rule):
	say msg-string.


Chapter Purging the queue

To purge the/-- (queue-object - a message queue):
	repeat through Table of Message Queue Data:
		if the queue name entry is the queue-object:
			blank out the whole row.

To purge all message/-- queues:
	blank out the whole of the Table of Message Queue Data.

To purge all/the (priority-level - a queue-priority) priority/-- messages from the/-- (queue-object - message queue):
	repeat through Table of Message Queue Data:
		if the queue name entry is the queue-object and the queue-msg-priority entry is priority-level:
			blank out the whole row.


Chapter Default queue

The main message queue is a message queue.

Every turn (this is the handle the main message queue rule):
	show the next message from the main message queue.


Trainer Message Queues ends here.
